package com.coolwen.spring.jdbc.service;

import com.coolwen.spring.jdbc.dao.IGroupDao;
import com.coolwen.spring.jdbc.dao.IUserDao;
import com.coolwen.spring.jdbc.model.Group;
import com.coolwen.spring.jdbc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author CoolWen
 * @version 2020-04-16 10:03
 */
@Service("userService")
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private IUserDao userDao;

    @Autowired
    private IGroupDao groupDao;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void add(User user, int gid) {
        userDao.add(user, gid);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void delete(int id) {
        userDao.delete(id);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User load(int id) {
        return userDao.load(id);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void addGroupAndUser(User user, Group group) {
        groupDao.add(group);
        int i = 1 / 0;
        userDao.add(user, group.getId());

    }
}
